package com.starbaba.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

@SuppressWarnings("unused")
public class ImageUtil {
	
	public String pictureToString(String path){
			String imgStr = null;
		try{
			  // 将图片转换成字符串
			  File imgFile = new File(path);
			  FileInputStream fis = new FileInputStream(imgFile);
			  byte[] bytes = new byte[fis.available()];
			  fis.read(bytes);
			  fis.close();
			  //imgStr = byte2hex(bytes);
			  Base64Utils baseUtils = new Base64Utils();
			  imgStr =  Base64Utils.getBASE64(bytes);
			  }catch(Exception e){
			  e.printStackTrace();
			 }
		return imgStr;
	}
	

	
	public static String byte2hex(byte[] b) // 二进制转字符串
    {  
       StringBuffer sb = new StringBuffer();  
        String stmp = "";  
        for (int n = 0; n < b.length; n++) {  
	           stmp = Integer.toHexString(b[n] & 0XFF);  
	           
	        if (stmp.length() == 1) {  
               sb.append("0" + stmp);  
            } else {  
               sb.append(stmp);  
            }  
       }  
       return sb.toString(); 
    }
	
	
	public static byte[] hex2byte(String s) {   
	    try {
			byte[] src = s.toLowerCase().getBytes("iso-8859-1");   
			byte[] ret = new byte[src.length / 2];   
			for (int i = 0; i < src.length; i += 2) {   
			    byte hi = src[i];   
			    byte low = src[i + 1];   
			    hi = (byte) ((hi >= 'a' && hi <= 'f') ? 0x0a + (hi - 'a')   
			            : hi - '0');   
			    low = (byte) ((low >= 'a' && low <= 'f') ? 0x0a + (low - 'a')   
			            : low - '0');   
			    ret[i / 2] = (byte) (hi << 4 | low);   
			}   
			return ret;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		return null;
	}
	
	
	public static void saveImage(String data, String fileName,String type) {   
		     
	    try {
	    	 Base64Utils baseUtils = new Base64Utils();
	    	 byte[] bytes = Base64Utils.getFromBASE64(data);
	        //byte[] bytes = hex2byte(data);   
	        System.out.println("path:" + fileName);   
	        RandomAccessFile file = new RandomAccessFile(fileName, "rw");   
	        file.write(bytes);   
	        file.close();   
	    } catch (IOException e) {   
	        e.printStackTrace();   
	    }   
	}  

	
	public  static void main(String[] args){
		
		ImageUtil imageutil = new ImageUtil();
		String  str = imageutil.pictureToString("C:\\Users\\Administrator\\Pictures\\media.jpg");
		ImageUtil.saveImage(str, "C:\\Users\\Administrator\\Pictures\\media1.jpg", "");
		//System.out.println(imageutil);
		//String a ="abc";
//		byte[] b ={1,12,34,56};
//		String str = imageutil.byte2hex(b);
//		byte[]c = imageutil.hex2byte(str);
//		System.out.println(c);
		//System.out.println(a.length());
		
		
//		try {
//			String str = "商斌";
//			byte[] src = str.toLowerCase().getBytes("utf-8");   
//			System.out.println(Arrays.toString(src));
//			
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
	}
	
}
